package com.shujia.java.day7;

public class InsertStringBuffe {
    public static void main(String[] args) {
        StringBuffer stringBuffer = new StringBuffer("123456789");

        //从后向前加 每隔3位加个逗号
        for (int i = stringBuffer.length() - 3; i > 0; i = i - 3) {
            //插入
            stringBuffer.insert(i, ",");
        }
        System.out.println(stringBuffer);
        System.out.println("--------------------------");
        StringBuffer str = new StringBuffer("123456789");
        //从前向后加
        for (int i = 3; i <= str.length(); i = i + 4) {
            str.insert(i,",");
            if(i+4>=str.length()){
                str.insert(str.length(),".");
                break;
            }
        }

//        //判断这两种情况
//        if (str.length() % 3 == 0) {
//            //替换
//            str.replace(str.length()-1,str.length(),".");
//        }else {
//            str.append(".");
//        }
        System.out.println(str);
    }
}
